package com.roads.workflow.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.roads.common.constant.Constants;
import com.roads.common.utils.DateUtils;
import com.roads.common.utils.SecurityUtils;
import com.roads.common.utils.uuid.IdUtils;
import com.roads.workflow.domain.WorkflowMeterageNode;
import com.roads.workflow.mapper.WorkflowMeterageNodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.roads.workflow.mapper.WorkflowMeterageDefinitionMapper;
import com.roads.workflow.domain.WorkflowMeterageDefinition;
import com.roads.workflow.service.IWorkflowMeterageDefinitionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 流程管理-计量流程定义Service业务层处理
 * 
 * @author nhp
 * @date 2022-05-30
 */
@Service
public class WorkflowMeterageDefinitionServiceImpl implements IWorkflowMeterageDefinitionService {

    @Autowired
    private WorkflowMeterageDefinitionMapper mapper;

    @Autowired
    private WorkflowMeterageNodeMapper workflowMeterageNodeMapper;

    /**
     * 根据主键ID查询详情
     * 
     * @param id 流程管理-计量流程定义主键
     * @return 流程管理-计量流程定义
     */
    @Override
    public WorkflowMeterageDefinition getModelById(String id) {
        return mapper.getModelById(id);
    }

    /**
     * 查询列表
     * 
     * @param model 流程管理-计量流程定义
     * @return 流程管理-计量流程定义
     */
    @Override
    public List<WorkflowMeterageDefinition> selectModelList(WorkflowMeterageDefinition model) {
        return mapper.selectModelList(model);
    }

    /**
     * 新增
     * 
     * @param model 流程管理-计量流程定义
     * @return 结果
     */
    @Override
    public int insert(WorkflowMeterageDefinition model) {
        model.setWorkflowUuid(IdUtils.simpleUuid());
        model.setCreateBy(SecurityUtils.getUsername());
        model.setCreateTime(DateUtils.getNowDate());
        return mapper.insert(model);
    }

    /**
     * 粘贴
     *
     * @param model 流程管理-计量流程定义
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int paste(WorkflowMeterageDefinition model) {
        String newWorkflowUuid = IdUtils.simpleUuid();
        // 流程定义表
        WorkflowMeterageDefinition original = mapper.getModelById(model.getWorkflowUuid());
        WorkflowMeterageDefinition paste = new WorkflowMeterageDefinition();
        paste.setWorkflowUuid(newWorkflowUuid);
        paste.setWorkflowName(model.getWorkflowName());
        paste.setRemark(model.getWorkflowName());
        paste.setProjectCode(original.getProjectCode());
        paste.setProjectName(original.getProjectName());
        paste.setWorkflowType(original.getWorkflowType());
        paste.setStatus("0");
        paste.setCreateBy(SecurityUtils.getUsername());
        paste.setCreateTime(DateUtils.getNowDate());
        mapper.insert(paste);
        // 流程节点表
        WorkflowMeterageNode search = new WorkflowMeterageNode();
        search.setWorkflowUuid(model.getWorkflowUuid());
        List<WorkflowMeterageNode> originalNodes = workflowMeterageNodeMapper.selectModelList(search);
        List<WorkflowMeterageNode> pasteNodes = new ArrayList<>();
        for (WorkflowMeterageNode originalNode : originalNodes) {
            String workflowNodeUuid = IdUtils.simpleUuid();
            WorkflowMeterageNode pasteNode = new WorkflowMeterageNode();
            pasteNode.setWorkflowUuid(newWorkflowUuid);
            pasteNode.setWorkflowNodeUuid(workflowNodeUuid);
            pasteNode.setWorkflowNodeName(originalNode.getWorkflowNodeName());
            pasteNode.setWorkflowNodeOrder(originalNode.getWorkflowNodeOrder());
            pasteNodes.add(pasteNode);
        }
        workflowMeterageNodeMapper.batchInsert(pasteNodes);
        return 1;
    }

    /**
     * 修改
     * 
     * @param model 流程管理-计量流程定义
     * @return 结果
     */
    @Override
    public int update(WorkflowMeterageDefinition model) {
        model.setUpdateBy(SecurityUtils.getUsername());
        model.setUpdateTime(DateUtils.getNowDate());
        return mapper.update(model);
    }

    /**
     * 批量删除
     * 
     * @param ids 主键集合
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        ////// 删除限制
        return mapper.deleteByIds(ids);
    }

    /**
     * 删除
     * 
     * @param id 流程管理-计量流程定义主键
     * @return 结果
     */
    @Override
    public int delete(String id) {
        return mapper.delete(id);
    }
}
